const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskDetail:'',
    taskTabType:1,
    deliverManuscriptList:[{}],
    commentList:[],
    orderDetail:'',
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.taskDetail()

  },



  /**
   * 关注发布者
   */
  userFocus(){
    if(app.globalData.token){
      let _this=this
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseUrl + 'task/focusUser',
        data: {
          focus_uid: _this.data.taskDetail.uid,
          token: app.globalData.token
        },
        success(res) {
          wx.hideLoading()
          if (res.data.code == 1000) {
            wx.showToast({
              title: '关注成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              _this.taskDetail();
            }, 2000)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else {
      wx.showModal({
        title: '提示',
        content: '您未登录，是否去登陆？',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/userPage/pages/login/login',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }

  },


  // 任务详情信息
  taskDetail:function(){
    var _this = this;
    var { id } = this.data;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'myTask/detail',
      data: {
        id:id,
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          _this.setData({
            taskDetail:res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },


  orderDetail(){
    var _this = this
    var url = app.globalData.baseUrl + 'myTask/rewardOrder';
    var { id } = this.data;
    var data = {
      id: id,
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data,(res) =>{
      wx.hideLoading()
      console.log(res)
      if (res.code == 1000){
        _this.setData({
          orderDetail:res.data
        })
      }
    }, (err) =>{
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 评论列表
  commentList: function () {
    var _this = this
    var url = app.globalData.baseUrl + 'task/commentList';
    var { id } = this.data;
    var data = {
      id: id,
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        _this.setData({
          commentList: res.data
        })
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 底部列表切换
  taskTabBtn:function(e){
    var { index } = e.currentTarget.dataset
    this.setData({
      taskTabType: index
    })
  },
  // 验收
  acceptanceBtn:function(e){
    var _this = this
    var url = app.globalData.baseUrl + 'work/deliveryAgree';
    var data = {
      work_id: e.currentTarget.dataset.id,
      status: e.currentTarget.dataset.type,
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        wx.showToast({
          title: '验收成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          _this.orderDetail();
        }, 2000)
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 维权
  protectionBtn:function(e){
    var { workid } = e.currentTarget.dataset
    var { id } = this.data
    wx.navigateTo({
      url: '/taskPage/pages/protection/protection?taskid=' + id + '&workid=' + workid,
    })
  },
  // 附件下载
  wxdown:function(event){
    var { down_url, annextype} = event.currentTarget.dataset;
    let _that = this;
    if (annextype != 'jpg' && annextype != 'png' && annextype != 'jpeg' && annextype != 'gif'){
      if (annextype != 'zip' && annextype != 'rar'){
        wx.downloadFile({
          url: down_url,
          success: function (res) {
            const tempFilePath = res.tempFilePath;
            // 保存文件
            wx.saveFile({
              tempFilePath,
              success: function (res) {
                const savedFilePath = res.savedFilePath;
                // 打开文件
                wx.openDocument({
                  filePath: savedFilePath,
                  showMenu:true,
                  success: function (res) {
                    console.log('打开文档成功')
                  },
                });
              },
              fail: function (err) {
                console.log('保存失败：', err)
              }
            });
          },
          fail: function (err) {
            console.log('下载失败：', err);
          },
        });
      }else{
        wx.showToast({
          title: '暂不支持改文件类型',
          icon: "none",
          duration: 2000
        })
      }
      return false;
    }else{
      //预览
      wx.previewImage({
        current: down_url, // 当前显示图片的http链接
        urls:[
          down_url
        ]
      })
    }
   },

  // 评价
  taskEvaluation:function(e){
    // console.log(e)
    var { type } = e.currentTarget.dataset
    var { id, orderDetail } = this.data
    var workid = ''
    if (type == 1){
      wx.navigateTo({
        url: '/taskPage/pages/taskEvaluation/taskEvaluation?type=' + type + '&taskid=' + orderDetail.id + '&workid=' + orderDetail.bidWorks[0].delivery.id + '&avatar=' + orderDetail.gz_avatar + '&title=' + orderDetail.gz_username,
      })
    }else{
      var { index } = e.currentTarget.dataset
      wx.navigateTo({
        url: '/taskPage/pages/taskEvaluation/taskEvaluation?type=' + type + '&taskid=' + orderDetail.id + '&workid=' + orderDetail.bidWorks[index].delivery.id + '&avatar=' + orderDetail.bidWorks[index].avatar + '&title=' + orderDetail.bidWorks[index].name,
      })
    }
  },
  // 威客交稿
  submitManuscript:function(){
    var { orderDetail } = this.data
    var jd = '';
    wx.navigateTo({
      url: '/taskPage/pages/submitTaskManuscript/submitTaskManuscript?id=' + orderDetail.id + '&jd=' + jd,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.orderDetail();
    this.commentList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
